package com.zhuiYun.project.api.commoditycollection.mapper;

import com.zhuiYun.project.api.commoditycollection.entity.CommodityCollection;
import com.zhuiYun.project.api.commoditycollection.entity.dto.CommodityCollectionDTO;
import com.zhuiYun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @Description TODO 商品收藏mapper
 * @ClassName CommodityCollection
 * @Author yz
 * @Date 2023/4/4 13:55
 **/
@Mapper
public interface CommodityCollectionMapper extends BaseMapper<CommodityCollection> {
/**
* @Author yangze
* @Description TODO 查询商品收藏
* @Date 2023/4/5 11:06
* @Return
* @param commodityCollectionDTO
*/

    @Select("SELECT\n" +
            "\tb.commodity_name,b.back,b.price,b.favorable_price,b.image_id,a.id \n" +
            "FROM\n" +
            "\t`commodity_collection` a\n" +
            "\tLEFT JOIN commodity b ON b.id = a.commodity_id \n" +
            "WHERE\n" +
            "\ta.user_id = #{userId}\n" +
            "\tAND b.commodity_name LIKE '%${commodityName}%'  AND a.del_flag = 0\n" +
            "ORDER BY\n" +
            "\ta.update_time DESC")
    List<CommodityCollectionDTO> selectByUserId(CommodityCollectionDTO commodityCollectionDTO);
    /**
    * @Author yangze
    * @Description TODO 商品取消收藏
    * @Date 2023/4/5 11:06
    * @Return
    * @param id
    */

    @Update("UPDATE commodity_collection SET del_flag = 1 WHERE id = #{id}")
    int deleteById(Integer id);

    /**
    * @Author 韩佳轩
    * @Description  根据userid和commodityId取消收藏
    * @Date 2023/4/10 17:44
    * @Return int
    * @param userId
    * @param commodityId
    */
    @Update("UPDATE commodity_collection SET del_flag = 1, update_time = NOW() WHERE user_id = #{userId} AND commodity_id = #{commodityId}")
    int deleteByUserIdAndCommodityId(Integer userId, Integer commodityId);


    /**
    * @Author yangze
    * @Description  TODO 查询用户是否收藏该商品
    * @Date 2023/4/10 15:39
    * @Return
    * @param userId
    * @param commodityId
    */

    @Select("SELECT * FROM commodity_collection WHERE user_id = #{userId} AND commodity_id = #{commodityId} ")
    CommodityCollection selectCollection(Integer userId, Integer commodityId);

    /**
    * @Author yangze
    * @Description TODO 修改商品收藏状态
    * @Date 2023/4/10 15:43
    * @Return
    * @param userId,commodityId
    */

    @Update("UPDATE commodity_collection SET del_flag = 0 WHERE user_id = #{userId} AND commodity_id = #{commodityId}")
    int updateCollection(Integer userId, Integer commodityId);

    /**
    * @Author yangze
    * @Description TODO 根据用户ID和商品ID取消收藏
    * @Date 2023/4/10 15:45
    * @Return
    * @param userId,commodityId
    */
//
//    @Update("UPDATE commodity_collection SET del_flag = 1 WHERE user_id = #{userId} AND commodity_id = #{commodityId}")
//    int deleteCollection(Integer userId, Integer commodityId);


    /**
     * @Author 赵笛
     * @Description TODO 根据当前用户id查询收藏商品个数
     * @Date 2023/4/10 20:07
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param userId
     */
    @Select("select count(id) " +
            "from commodity_collection " +
            "where user_id = #{userId} " +
            "and del_flag = 0")
    Integer getCommodityCollectionCountByUserId(Integer userId);
}
